package com.lkpstudy.controller;

import com.lkpstudy.pojo.Article;
import com.lkpstudy.pojo.PageBean;
import com.lkpstudy.pojo.Result;
import com.lkpstudy.service.ArticleService;
import com.lkpstudy.utils.JwtUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/article")
@CrossOrigin(origins = "http://localhost:5173",allowCredentials = "true") //允许跨域
@AllArgsConstructor
public class ArticleController {
    private ArticleService articleService;

    @PostMapping
    public Result add(@RequestBody @Validated(Article.add.class) Article article) {
        articleService.add(article);
        return Result.success();
    }

    @GetMapping
    public Result<PageBean<Article>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state
    ) {
        PageBean<Article> pb = articleService.list(pageNum,pageSize,categoryId,state);
        return Result.success(pb);
    }

    @GetMapping("/detail")
    public Result getContent(@RequestParam Integer id) {
        Article article = articleService.getContent(id);
        return Result.success(article);
    }

    @PutMapping
    public Result update(@RequestBody @Validated(Article.update.class) Article article) {
        articleService.update(article);
        return Result.success();
    }

    @DeleteMapping
    public Result delete(@RequestParam Integer id) {
        articleService.delete(id);
        return Result.success();
    }
}
